﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for ChatRoom
/// </summary>
public class ChatRoom
{
    private String _id;

    //Data Access class for chatroom
    private ChatRoomDA room;

    //Data Access class for room members
    private ChatRoomMembersDA roomMembers;

    //Data Access class for chat message
    private ChatMessageDA roomMsgs;

	public ChatRoom()
	{
        room = new ChatRoomDA();
        roomMembers = new ChatRoomMembersDA();
        roomMsgs = new ChatMessageDA();
	}

    public String ID
    {
        get { return _id; }
        set { _id = value; }
    }

    public String Create(String title, String creatorID)
    {
        //returns the id of the newly-created room
        String id = room.Create(title, creatorID);
        this.ID = id;
        return id;
    }

    public int Close()
    {
        return room.Close(this);
    }

    public int Join(String userID)
    {
        return roomMembers.Join(this,userID);
    }

    public int Leave(String userID)
    {
        return roomMembers.Leave(this,userID);
    }

    public int SendMessage(ChatMessage msg)
    {
        return roomMsgs.Send(msg);
    }

    public String ReadMessage()
    {
        String messages = "";
        DataTable ds = roomMsgs.Read(this);
        foreach (DataRow row in ds.Rows)
        {
            messages = messages +  row["userRealName"] + ": " + row["message"] + "\n";
        }
        return messages;
    }

    public String GetOtherUserInRoom(String currentUserID)
    {
        return room.GetOtherUserInRoom(this, currentUserID);
    }

    public Boolean StillHasMembers()
    {
        return room.StillHasMembers(this);
    }
}
